home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-21 | 4.2 KB | 143 lines | [TEXT/MPS ] |
- (*
- splitByWord(string,firstWord,lastWord) - Take a string containing a number of lines, and return a string
- with the same number of lines, each one containing word firstWord through word lastWord of the original
- line.
-
- To compile and link this file using Macintosh Programmers Workshop,
-
- pascal -w splitByWord.p
- link -m ENTRYPOINT -o HyperCommands -rt XFCN=7871 -sn Main=splitByWord ∂
- splitByWord.p.o "{Libraries}HyperXLib.o" "{MPW}"Libraries:interface.o "{MPW}"Libraries:PLibraries:PasLib.o
-
- © Copyright 1989 by Apple Computer, Inc.
-
- Initial coding 3/24/89 by Harry R. Chesley.
- *)
-
- {$R-}
-
- {$S splitByWord } { Segment name must be the same as the command name. }
-
- unit DummyUnit;
-
- interface
-
- uses MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- implementation
-
- const
-
- kReturn = 13; { ASCII for carriage return. }
-
- procedure splitByWord(paramPtr: XCmdPtr); forward;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- begin
- splitByWord(paramPtr);
- end;
-
- procedure splitByWord(paramPtr: XCmdPtr);
-
- var str: Str255;
- sourceHand: Handle; { Handle to the original source text. }
- firstWord: integer; { Which word to start with. }
- lastWord: integer; { Which word to end with. }
- resultHand: Handle; { Handle to the result text. }
- resultPtr: Ptr;
- resultSize: longInt; { Size of the result. }
-
- procedure Fail(errMsg: Str255); { set theResult and quit }
- begin
- paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
- exit(splitByWord);
- end;
-
- procedure processSource(procedure makeLine(linePtr: Ptr; lineSize: longInt));
- { Run through the input and call makeLine for each line in it. }
-
- var p: Ptr; { Input pointer. }
- copyStart: Ptr; { Pointer to beginning of section to copy. }
- i: integer;
-
- begin
- { Start at the start. }
- p := sourceHand^;
- while p^ <> 0 do
- begin
- { Find the beginning of the first word to copy. }
- i := 1;
- while i < firstWord do
- begin
- while p^ = ord(' ') do p := Ptr(ord4(p)+1);
- while (p^ <> ord(' ')) and (p^ <> kReturn) and (p^ <> 0) do p := Ptr(ord4(p)+1);
- i := i+1;
- end;
- while p^ = ord(' ') do p := Ptr(ord4(p)+1);
- copyStart := p;
- { Find the end of the last word to copy. }
- while i <= lastWord do
- begin
- while p^ = ord(' ') do p := Ptr(ord4(p)+1);
- while (p^ <> ord(' ')) and (p^ <> kReturn) and (p^ <> 0) do p := Ptr(ord4(p)+1);
- i := i+1;
- end;
- { Call makeLine with the part of the line to be copied. }
- makeLine(copyStart,ord4(p)-ord4(copyStart));
- { Skip to the end of the line. }
- while (p^ <> kReturn) and (p^ <> 0) do p := Ptr(ord4(p)+1);
- if p^ = kReturn then p := Ptr(ord4(p)+1);
- end;
- end;
-
- procedure countSizes(linePtr: Ptr; lineSize: longInt);
- { Routine to pass to processInput to figure the size of the output. }
-
- begin
- resultSize := resultSize + lineSize + 1;
- end;
-
- procedure storeLine(linePtr: Ptr; lineSize: longInt);
- { ROutine to pass to processInput to copy the result into the output. }
-
- begin
- BlockMove(linePtr,resultPtr,lineSize);
- resultPtr := Ptr(ord4(resultPtr)+lineSize);
- resultPtr^ := kReturn;
- resultPtr := Ptr(ord4(resultPtr)+1);
- end;
-
- begin
- if paramPtr^.paramCount <> 3 then Fail('§§§ parameter count is not 3 §§§');
-
- sourceHand := paramPtr^.params[1]; { First parameter is the string to be grouped. }
- ZeroToPas(paramPtr,paramPtr^.params[2]^,str); { Second parameter is the number of the first word to copy. }
- firstWord := StrToNum(paramPtr,str);
- if firstWord < 1 then firstWord := 1;
- ZeroToPas(paramPtr,paramPtr^.params[3]^,str); { Third parameter is the number of the last word to copy. }
- lastWord := StrToNum(paramPtr,str);
-
- { If there's anything in the string, process it. }
- if sourceHand <> NIL then
- begin
- { Figure out the size of the result (including the null termination). }
- resultSize := 1;
- processSource(countSizes);
- if resultSize = 1 then Fail('');
- { Allocate the result handle. }
- resultHand := NewHandle(resultSize);
- if memError <> noErr then Fail('§§§ could not allocate result §§§');
- { Copy in the data. }
- resultPtr := resultHand^;
- processSource(storeLine);
- resultPtr^ := 0;
- paramPtr^.returnValue := resultHand;
- end
- else Fail('');
- end;
-
- end.
-